<?php

class Application_Model_User extends Dealscombined_Models_ModelAbstract {

	protected $_id;
	protected $_firstname;
	protected $_lastname;
	protected $_cityId;
	protected $_email;
	protected $_password;
	protected $_sessionId;
	protected $_lastSeen;
	protected $_lastLogin;
	protected $_status = 0;
	protected $_roles = 0;
	protected $_language;

	protected $_lastIp;
	protected $_lastHost;
	protected $_lastUserAgent;
	protected $_createdAt;

	protected $_rank;
	protected $_magic;
	protected $_authenticated;
	protected $_cookieName;
	
	protected $_facebookProfile;
	protected $_logOutUrl;
	protected $_logInUrl;
	

	public function setId($id) {
		$this -> _id = $id;
		return $this;
	}

	public function getId() {
		return $this -> _id;
	}

	public function getFullname() {
		return $this -> _firstname . ' ' . $this -> _lastname;
	}

	public function setFirstname($firstname) {
		$this -> _firstname = $firstname;
		return $this;
	}

	public function getFirstname() {
		return $this -> _firstname;
	}

	public function setLastname($lastname) {
		$this -> _lastname = $lastname;
		return $this;
	}

	public function getLastname() {
		return $this -> _lastname;
	}

	public function setCityId($cityId) {
		$this ->_cityId = $cityId;
		return $this;
	}

	public function getCityId() {
		return $this ->_cityId;
	}
	
	public function setEmail($email) {
		$this -> _email = $email;
		return $this;
	}

	public function getEmail() {
		return $this -> _email;
	}

	public function setPassword($password) {
		$this -> _password = $password;
		return $this;
	}

	public function getPassword() {
		return $this -> _password;
	}

	public function setSessionId($sessionId) {
		$this -> _sessionId = $sessionId;
		return $this;
	}

	public function getSessionId() {
		return $this -> _sessionId;
	}
	
	public function setLastSeen($lastSeen) {
		$this -> _lastSeen = $lastSeen;
		return $this;
	}

	public function getLastSeen() {
		return $this -> _lastSeen;
	}
	
	public function setLastLogin($lastLogin) {
		$this -> _lastLogin = $lastLogin;
		return $this;
	}

	public function getLastLogin() {
		return $this -> _lastLogin;
	}

	public function setStatus($status) {
		$this -> _status = $status;
		return $this;
	}

	public function getStatus() {
		return $this -> _status;
	}

	/*public function setRoles(array $roles) {
		$this -> _roles = $roles;
		return $this;
	}

	public function getRoles() {
		return $this -> _roles;
	}*/

	public function setDataset($dataset) {
		$this -> _dataset = $dataset;
		return $this;
	}

	public function getDataset() {
		return $this -> _dataset;
	}

	public function setLanguage($language) {
		$this -> _language = $language;
		return $this;
	}

	public function getLanguage() {
		return $this -> _language;
	}

	public function getLastIp() {
		return $this -> _lastIp;
	}

	public function getLastHost() {
		return $this -> _lastHost;
	}

	public function getLastUserAgent() {
		return $this -> _lastUserAgent;
	}

	public function getCreatedAt() {
		return $this -> _createdAt;
	}

	public function getRank() {
		return $this -> _rank;
	}

	public function getMagic() {
		return $this -> _magic;
	}

	public function getAuthenticated() {
		return $this -> _authenticated;
	}

	public function getCookieName() {
		return $this -> _cookieName;
	}

	public function setLastIp($lastIp) {
		$this -> _lastIp = $lastIp;
	}

	public function setLastHost($lastHost) {
		$this -> _lastHost = $lastHost;
	}

	public function setLastUserAgent($lastUserAgent) {
		$this -> _lastUserAgent = $lastUserAgent;
		return $this;
	}

	public function setCreatedAt($createdAt) {
		$this -> _createdAt = $createdAt;
		return $this;
	}

	public function setRank($rank) {
		$this -> _rank = $rank;
		return $this;
	}

	public function setMagic($magic) {
		$this->_magic = $magic;
		return $this;
	}

	public function setAuthenticated($authenticated) {
		$this -> _authenticated = $authenticated;
		return $this;
	}

	public function setCookieName($cookieName) {
		$this -> _cookieName = $cookieName;
		return $this;
	}

	public function touch() {

		$this -> setLastIp($_SERVER['REMOTE_ADDR']);
		$this -> setLastHost(gethostbyname($_SERVER['REMOTE_ADDR']));
		$this -> setLastUserAgent($_SERVER['HTTP_USER_AGENT']);
		//$this -> setLastLogin(date("Y-m-d H:i:s"));
		$this->setLastSeen(date("Y-m-d H:i:s"));
		/*if ($blnUpdateDB) {
		 $objResult = mysql_query("
		 UPDATE `users` SET
		 `last_ip` = '".$DB->makeSafe($this->last_ip)."',
		 `last_host` = '".$DB->makeSafe($this->last_host)."',
		 `last_user_agent` = '".$DB->makeSafe($this->last_user_agent)."',
		 `last_seen` = '".$DB->makeSafe($this->last_seen)."'
		 WHERE `id` = '".$DB->makeSafe($this->id)."' LIMIT 1
		 ");
		 }*/
	}

	// generate magic for cookie
	public function generateMagic() {
		return (string) uniqid("DCUK", true) . md5('edgoniugv256yuj' . mt_rand(0, 8391491329));
	}

	// hash password
	public static function hashPassword($strPassword) {
		return sha1('LMHUB.6triuygsaa.' . $strPassword);
	}

	// check cookie and instantiate if necessary
	public static function authenticate() {
		/*global $DB;

		 if (isset($_COOKIE[self::$cookie_name])) {
		 // check the magic string in the cookie against the user table (only enabled users)
		 $objResult = mysql_query("SELECT `id` FROM `users` WHERE `magic` = '".$DB->makeSafe($_COOKIE[self::$cookie_name])."' LIMIT 1");
		 if ($objResult && mysql_num_rows($objResult) > 0) {
		 $arrRow = mysql_fetch_array($objResult);

		 $objCurrentUser = new User();
		 $objCurrentUser->read($arrRow['id']);
		 $objCurrentUser->authenticated = true;
		 $objCurrentUser->touch(true);
		 return $objCurrentUser;
		 }
		 else {
		 return false;
		 }
		 }
		 else {
		 return false;
		 }*/
	}

	// attempt to log a user in
	// we invalidate old sessions on each login
	public static function login($strEmail = false, $strPasswordPlain = false) {
		/*global $DB;

		 if ($strEmail !== false && $strPasswordPlain !== false) {

		 $strPasswordHashed = self::hashPassword($strPasswordPlain);

		 // check the username and password against the database, and if OK, set cookie
		 $objResult = mysql_query("SELECT `id` FROM `users` WHERE `email` = '".$DB->makeSafe($strEmail)."' AND `password` = '".$DB->makeSafe($strPasswordHashed)."' LIMIT 1");
		 if ($objResult && mysql_num_rows($objResult) > 0) {
		 $arrRow = mysql_fetch_array($objResult);

		 // generate new magic for user and save
		 $strNewMagic = self::generateMagic();
		 $objResult = mysql_query("UPDATE `users` SET `magic` = '".$DB->makeSafe($strNewMagic)."' WHERE `id` = '".intval($arrRow['id'])."' LIMIT 1");

		 // set cookie to expire a year from now
		 return setcookie(self::$cookie_name, $strNewMagic, time() + 60*60*24*365);
		 }
		 else {
		 return false;
		 }
		 }
		 else {
		 return false;
		 }*/
	}
	
	public function getFacebookProfile(){
		return $this->_facebookProfile;
	}

	public function setFacebookProfile($facebookProfile){
		$this->_facebookProfile = $facebookProfile;
	}

	public function getLogOutUrl(){
		return $this->_logOutUrl;
	}

	public function setLogOutUrl($logOutUrl){
		$this->_logOutUrl = $logOutUrl;
	}

	public function getLogInUrl(){
		return $this->_logInUrl;
	}

	public function setLogInUrl($logInUrl){
		$this->_logInUrl = $logInUrl;
	}
	

}
